
#if UNIT_TEST

#region Imports

using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;

using Justa.Messaging.Routing;

#endregion

namespace Justa.Messaging.Test.Routing
{
    /// <summary>
    /// 
    /// </summary>
    /// 
    [TestFixture]
    public class ContentBasedRouterTest
    {
        /// <summary>
        /// 
        /// </summary>
        /// 
        [Test]
        public void TestRoutingRules()
        {
            // Send a message to the "justa.messaging.routing.source" queue.
            IMessageGateway<string> gateway = new MessageGateway<string>("justa.messaging.routing.source");
            gateway.Send("route");

            // Setup a router with a simple rule that sends messages from the "justa.messaging.routing.source"
            // queue to the "justa.messaging.routing.source.routed" queue when the contents of the message are 
            // equals "route" otherwise send the message to the "justa.messaging.routing.source.notrouted" queue.
            //
            ContentBasedRouter<string> router = new ContentBasedRouter<string>("justa.messaging.routing.source");
            router.AddRule(new RoutingRule<string>(delegate(string message)
            {
                if (message.Equals("route"))
                {
                    return new string[] { "justa.messaging.routing.source.routed" };
                }
                else
                {
                    return new string[] { "justa.messaging.routing.source.notrouted" };
                }
            }));

            // Start the router and let it run for 1 second.
            router.Start();
            System.Threading.Thread.Sleep(1000);
            router.Stop();

            // Receive the expected message from the target destination "justa.messaging.routing.source.routed" 
            // defined in the routing rule.
            //
            IMessageGateway<string> receiver = new MessageGateway<string>("justa.messaging.routing.source.routed");
            string response = receiver.Receive(1000);

            Assert.IsNotNull(response);
            Assert.AreEqual("route", response);
            Console.WriteLine("Routed message: {0}.", response);
        }
    }
}

#endif
